# -*- coding: utf-8 -*-
from django.contrib import admin
from promotion.models import Promotion, AwardedEmployee
from datetime import datetime

class AwardedEmployeeInline(admin.TabularInline):
    model = AwardedEmployee
    extra = 0
    
class PromotionAdmin(admin.ModelAdmin):
    inlines = [AwardedEmployeeInline,]
    list_display = ['title', 'note', 'awarded_count']
    list_display_links = ['title', 'awarded_count']

    def awarded_count(self, obj):
        return u'[ %s ] - посмотреть список' % obj.get_awarded().count()
    awarded_count.short_description = u'Количество награждённых'


admin.site.register(Promotion, PromotionAdmin)

class AwardedEmployeeAdmin(admin.ModelAdmin):
    list_display = ['employee', 'promotion', 'award_date']
    list_display_links = ['employee', 'promotion']
    list_filter = ['award_date']
    ordering = ['-award_date']

    def changelist_view(self, request, extra_context=None): # http://goo.gl/fH5VO o_O
        test = request.META['HTTP_REFERER'].split(request.META['PATH_INFO'])
        if test[-1] and not test[-1].startswith('?'):
            q = request.GET.copy()
            if not request.GET.has_key('award_date'):
                q['award_date'] = datetime.now().year
            request.GET = q
            request.META['QUERY_STRING'] = request.GET.urlencode()
        return super(AwardedEmployeeAdmin,self).changelist_view(request, extra_context=extra_context)

#admin.site.register(AwardedEmployee, AwardedEmployeeAdmin)